package com.inforich.web.controller.carManage;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Strings;
import com.inforich.carManage.domain.Driver;
import com.inforich.carManage.service.IDriverService;
import com.inforich.common.annotation.Log;
import com.inforich.common.core.controller.BaseController;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.BusinessType;
import com.inforich.common.utils.poi.ExcelUtil;

import cn.hutool.core.util.StrUtil;

/**
 * 司机Controller
 *
 * @author hjm
 * @date 2022-10-24
 */
@RestController
@RequestMapping("/carManage/driver")
public class DriverController extends BaseController {

  @Autowired
  private IDriverService driverService;

  /**
   * 车辆列表
   */
  @GetMapping("/all")
  public AjaxResult all(@RequestParam(required = false) String name) {
    List<Driver> list =
        driverService.list(
            new LambdaQueryWrapper<Driver>().like(StrUtil.isNotBlank(name), Driver::getName, name));
    return AjaxResult.success(list);
  }

  /**
   * 查询司机列表
   */
  @PreAuthorize("@ss.hasPermi('carManage:driver:list')")
  @GetMapping("/list")
  public Page<Driver> list(Page<Driver> page, Driver driver) {
    Map<String, Object> map = beanToMap(driver);
    driverService.pageDriver(page, map);
    List<Driver> drivers = page.getRecords();
    if (drivers != null && drivers.size() > 0) {
      drivers.forEach(x -> x.setIdnumber(desensitizedIdNumber(x.getIdnumber())));
    }
    return page;
  }

  /**
   * 导出司机列表
   */
  @PreAuthorize("@ss.hasPermi('carManage:driver:export')")
  @Log(title = "司机", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, Driver driver) {
    Map<String, Object> map = beanToMap(driver);
    List<Driver> list = driverService.listDriver(map);
    if (list != null && list.size() > 0) {
      list.stream().forEach(x -> x.setIdnumber(desensitizedIdNumber(x.getIdnumber())));
    }
    ExcelUtil<Driver> util = new ExcelUtil<Driver>(Driver.class);
    util.exportExcel(response, list, "司机数据");
  }

  /**
   * 获取司机详细信息
   */
  @PreAuthorize("@ss.hasPermi('carManage:driver:query')")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") Long id) {
    return AjaxResult.success(driverService.getById(id));
  }

  /**
   * 新增司机
   */
  @PreAuthorize("@ss.hasPermi('carManage:driver:add')")
  @Log(title = "司机", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@Validated @RequestBody Driver driver) {
    String checkData = driverService.checkData(driver);
    if (StrUtil.isNotBlank(checkData)) {
      return AjaxResult.error(checkData);
    }
    return driverService.addDriver(driver);
  }

  /**
   * 修改司机
   */
  @PreAuthorize("@ss.hasPermi('carManage:driver:edit')")
  @Log(title = "司机", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@Validated @RequestBody Driver driver) {
    String checkData = driverService.checkData(driver);
    if (StrUtil.isNotBlank(checkData)) {
      return AjaxResult.error(checkData);
    }
    return driverService.updateDriver(driver);
  }

  /**
   * 删除司机
   */
  @PreAuthorize("@ss.hasPermi('carManage:driver:remove')")
  @Log(title = "司机", businessType = BusinessType.DELETE)
  @DeleteMapping("/{id}")
  public AjaxResult remove(@PathVariable Long id) {
    return driverService.deleteDriver(id);
  }

  private String desensitizedIdNumber(String idNumber) {
    if (!Strings.isNullOrEmpty(idNumber)) {
      return StringUtils.left(idNumber, 6).concat(StringUtils.removeStart(
          StringUtils.leftPad(StringUtils.right(idNumber, 3), StringUtils.length(idNumber), "*"),
          "******"));
    }
    return idNumber;
  }
}
